import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import { HomePath } from '@/config/constants'
import Login from './modules/login'
import Layout from '@/layouts/index.vue'
import { usePermissionStore } from '@/store'
import ParentView from '@/components/ParentView/index.vue'

export const routes: RouteRecordRaw[] = [
  {
    path: '/',
    meta: { title: '公告板', hidden: true },
    redirect: HomePath,
  },
  {
    path: HomePath,
    component: Layout,
    name: 'Index',
    meta: {
      title: '首页',
    },
    children: [
      {
        path: HomePath,
        name: 'dashboard',
        component: () => import('@/views/basic/dashboard/index.vue'),
        meta: {
          title: '公告板',
          icon: 'icon-dashboard',
        },
      },
    ],
  },
  {
    path: '/error',
    name: 'error',
    meta: {
      title: '错误页面',
      icon: 'icon-close-circle',
      hidden: true,
    },
    redirect: '/error/404',
    component: Layout,
    children: [
      {
        path: '/error/404',
        name: '404',
        component: () => import('@/views/error/404/index.vue'),
        meta: {
          title: '404',
          icon: 'icon-file',
        },
      },
      {
        path: '/error/403',
        name: '403',
        component: () => import('@/views/error/403/index.vue'),
        meta: {
          title: '403',
          icon: 'icon-file',
        },
      },
    ],
  },
  {
    path: '',
    name: 'articles-edit',
    meta: {
      title: '新增 - markdown',
      icon: 'icon-close-circle',
      hidden: true,
    },
    component: ParentView,
    children: [
      {
        path: '/articles/blog/list/edit-md',
        name: 'articles-edit-page',
        component: () => import('@/views/articles/blog/list/edit-md/index.vue'),
        meta: {
          title: '404',
          icon: 'icon-file',
        },
      },
    ],
  },
  {
    path: '/:catchAll(.*)',
    name: '404',
    meta: {
      hidden: true,
    },
    component: () => import('@/views/error/404/index.vue'),
  },
  Login,
]

const router = createRouter({
  history: createWebHashHistory(import.meta.env.VITE_BASE_URL as string),
  routes,
})

// 重置路由
export function resetRouter() {
  const permissionStore = usePermissionStore()
  permissionStore.routes.forEach((route: any) => {
    const name = route.name
    if (name && router.hasRoute(name)) {
      router.removeRoute(name)
    }
  })
}

export default router
